package com.swak.persistence.mybatis.methods.impls;

import com.swak.persistence.mybatis.methods.AbstractMethod;
import com.swak.persistence.mybatis.methods.Sqls;
import com.swak.utils.StringUtils;

/**
 * 删除语句
 * 
 * @author lifeng
 * @date 2021年1月21日 下午5:31:56
 */
public class Delete extends AbstractMethod {

	@Override
	protected void doInject() {
		if (!this.supportMethod(Sqls.DELETE)) {
			return;
		}
		StringBuilder sql = new StringBuilder();
		String logicSql = this.parseUpdateProperty();
		if (StringUtils.isNotBlank(logicSql)) {
			sql.append(UPDATE).append(SPACE).append(this.table.getName()).append(SPACE).append(SET).append(SPACE)
					.append(logicSql).append(SPACE).append(WHERE).append(SPACE).append(this.parsePkEqualColumns())
					.toString();
		} else {
			sql.append(DELETE).append(SPACE).append(FROM).append(SPACE).append(this.table.getName()).append(SPACE)
					.append(WHERE).append(SPACE).append(this.parsePkEqualColumns()).toString();
		}
		this.addDeleteMappedStatement(Sqls.DELETE.getMethod(), sql.toString());
	}

	/**
	 * 解析逻辑更新列
	 */
	protected String parseUpdateProperty() {
		StringBuilder sql = new StringBuilder();
		if (this.table.logicDelColumn != null) {
			sql.append(this.table.logicDelColumn.name).append(EQUALS).append(ONE).append(SPLIT);
			sql.append(this.table.logicDelAtColumn.name).append(EQUALS).append(HASH_LEFT_BRACE)
					.append(this.table.logicDelAtColumn.javaPropertyEl).append(RIGHT_BRACE);
		}
		return sql.toString();
	}
}
